//
//  AddRoomController.m
//  YSSmartHome
//
//  Created by yanghao on 2016/10/9.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "AddOrEditRoomController.h"
#import "MQTTSessionManager.h"
#import "RoomModel.h"
#import "UIView+HUD.h"

@interface AddOrEditRoomController ()
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UITextField *nameText;
@property(nonatomic, strong)UIButton *setPicBtn;
@property(nonatomic, strong)UILabel *picLabel;
@property(nonatomic, assign)AddOrEditTypeControllerType type;
@property(nonatomic, strong)NSMutableArray * rooms;
@end

@implementation AddOrEditRoomController


- (AddOrEditRoomController *)initWithType:(AddOrEditTypeControllerType)VCType
{
    if (self = [super init]) {
        self.type = VCType;
        [self.view addSubview:self.nameLabel];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.view);
            make.top.equalTo(10);
            make.width.equalTo(kIphoneSize_Widith(70));
            make.height.equalTo(kIphoneSize_Height(55));
        }];
        [self.view addSubview:self.nameText];
        [self.nameText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.nameLabel);
            make.leading.equalTo(self.nameLabel.trailing);
            make.trailing.equalTo(self.view);
        }];
        [self.view addSubview:self.setPicBtn];
        [self.setPicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.nameLabel.bottom).offset(10);
            make.leading.equalTo(self.view).offset(10);
            make.trailing.equalTo(self.view).offset(-10);
            make.height.equalTo(kIphoneSize_Height(168));
        }];
        self.navigationItem.title = @"添加房间";
        if (self.type == AddOrEditTypeControllerTypeEdit) {
            self.navigationItem.title = @"编辑房间";
            [self.setPicBtn setBackgroundImage:[UIImage imageNamed:@"home_top"] forState:UIControlStateNormal];
            [self.setPicBtn setImage:nil forState:UIControlStateNormal];
            [self.setPicBtn addSubview:self.picLabel];
            
            [self.picLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self.setPicBtn);
            }];
        }
      self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *roomPath = [MQTTSessionManager roomListPath];
    self.rooms = [NSKeyedUnarchiver unarchiveObjectWithFile:roomPath];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)save {
    if (self.nameText.text.length == 0)
    {
        [self.view yh_showMsgHUD:@"名字不能为空"];
        return;
    }
    for (NSString *room in self.rooms) {
        if ([room isEqualToString:self.nameText.text]) {
            [self.view yh_showMsgHUD:@"房间已经存在了"];
            return;
        }
    }
    [self.rooms addObject:self.nameText.text];
    NSString *roomPath = [MQTTSessionManager roomListPath];
    [NSKeyedArchiver archiveRootObject:self.rooms toFile:roomPath];
    RoomModel *room = [[RoomModel alloc]initWithTitle:self.nameText.text];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"kZTAddNewRoomNotifKey" object:room];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - action
- (void)changeImage
{
    debugLogInfo(@"changeImage...");
}
#pragma mark  - lazy loading

- (UITextField *)nameText{
    if (!_nameText ) {
        _nameText = [[UITextField alloc]init];
        _nameText.placeholder = @"请输入房间名称";
        _nameText.backgroundColor = [UIColor whiteColor];
        _nameText.font = [UIFont systemFontOfSize:14];
    }
    return _nameText;
}
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel  = [[UILabel alloc]init];
        _nameLabel.text = @"名称";
        _nameLabel.textColor = RGBColor(51, 51, 51);
        _nameLabel.backgroundColor = [UIColor whiteColor];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

- (UILabel *)picLabel
{
    if (!_picLabel) {
        _picLabel  = [[UILabel alloc]init];
        _picLabel.text = @"点击更换图片";
        _picLabel.textColor = [UIColor whiteColor];
        _picLabel.textAlignment = NSTextAlignmentCenter;
        _picLabel.userInteractionEnabled = NO;
    }
    return _picLabel;
}
    
- (UIButton *)setPicBtn
{
    if (!_setPicBtn) {
        _setPicBtn = [[UIButton alloc]init];
        _setPicBtn.backgroundColor = [UIColor whiteColor];
        [_setPicBtn setImage:[UIImage imageNamed:@"me_icon_addroom"] forState:UIControlStateNormal];
        [_setPicBtn  addTarget:self action:@selector(changeImage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _setPicBtn;
}

@end
